// pages/center_bindcall/center_bindcall.js

import config from './../../config';
import {
    empty,
    message,
    session,
    getOpenId,
} from './../../utils/helper';
import {
    submitAdvisory,
    getPhoneNumber
} from './../../services/center_bindcall';
import WxValidate from './../../utils/validate';

//获取应用实例
const app = getApp();
//实例化本地缓存
const cookie = new session();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        tip: true,
        errorMsg: [],
        disabled: false,
        mobile_value: '',
        disabledMobile: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        //显示当前页面的转发按钮
        wx.showShareMenu({
            withShareTicket: true
        });
        this.initValidate();
    },

    //获取电话号码
    getphonenumber: function(e) {
        if (e.detail.errMsg == 'getPhoneNumber:ok') {
            let that = this;
            that.setData({
                disabledMobile: true
            })
            delete e.detail.errMsg;
            getOpenId(openid => {
                e.detail.openid = openid;
                getPhoneNumber(e.detail, {
                    success: (res) => {
                        that.setData({
                            errorMsg: [],
                            tip: true,
                            mobile_value: res.data.phoneNumber
                        })
                    },
                    complete: () => {
                        that.setData({
                            disabledMobile: false
                        })
                    }
                });
            });
        }
    },

    //校验表单函数
    checkForm: function(e) {
        const params = e.detail.value;
        // 传入表单数据，调用验证方法
        if (this.WxValidate.checkForm(params)) {
            this.setData({
                errorMsg: [],
                tip: true,
            });
            this.submitForm(params);
        } else {
            let errors = this.WxValidate.validationErrors(),
                error = [];
            error[0] = errors[0];
            this.setData({
                errorMsg: error,
                tip: false,
            });
        }
        return false;
    },

    submitForm(params) {
        this.setData({
            errorMsg: [],
            tip: false,
            disabled: true
        });
        let that = this;
        getOpenId(openid => {
            params.openid = openid;
            submitAdvisory(params, {
                success: (res) => {
                    that.setData({
                        mobile_value: ''
                    });
                    message('绑定成功', () => {
                        const len = getCurrentPages().length;
                        if (len > 1) {
                            wx.navigateBack({
                                delta: 1
                            })
                        }
                    });
                },
                complete: () => {
                    that.setData({
                        disabled: false
                    })
                }
            })
        })
    },

    //初始化验证规则
    initValidate() {
        // 验证字段的规则
        const rules = {
            mobile: {
                required: true,
                tel: true
            }
        }
        // 验证字段的提示信息，若不传则调用默认的信息
        const messages = {
            mobile: {
                required: '请填写手机号码',
                tel: '请填写有效的11位手机号码'
            }
        }
        // 创建实例对象
        this.WxValidate = new WxValidate(rules, messages)
    },

    //下拉刷新事件
    onPullDownRefresh() {
        this.onLoad(this.params);
        wx.stopPullDownRefresh();
    }
})